<?php
	if(isset($_SESSION['volunteer_id'])) {
	if($_POST['submit'] == 'Submit Form') {
	
		$err = array();
		$suc = array();
		$warn = array();
		
		$volunteer_email = mysql_real_escape_string($_POST['volunteer_email']);
		$volunteer_password = mysql_real_escape_string($_POST['volunteer_password']);
		$volunteer_confirm_password = mysql_real_escape_string($_POST['volunteer_confirm_password']);		
		$volunteer_firstname = mysql_real_escape_string($_POST['volunteer_firstname']);
		$volunteer_lastname = mysql_real_escape_string($_POST['volunteer_lastname']);
		$volunteer_ethnicity = mysql_real_escape_string($_POST['volunteer_ethnicity']);
		$volunteer_sex = mysql_real_escape_string($_POST['volunteer_sex']);
		$volunteer_acl_control_id = mysql_real_escape_string($_POST['volunteer_acl_control_id']);
		
		$set_password = md5($volunteer_password);
		
		if($volunteer_password <> $volunteer_confirm_password) {
			$warn[] = 'Las contraseñas no coinciden!';
		}
		
		$sql = "SELECT * FROM volunteers WHERE volunteer_email = '$volunteer_email'";
		$count = mysql_num_rows(mysql_query($sql));
		
		if($count >= 1) {
			$warn[] = 'La dirección de email ya se encuentra registrada!';
		}
		
		if($_POST['volunteer_ethnicity'] == 'null') {
			$warn[] = 'Debes seleccionar un origen etnico!';
		}
		
		if($_POST['volunteer_sex'] == 'null') {
			$warn[] = 'Debe seleccionar un sexo!';
		}
		
		if($_POST['volunteer_acl_control_id'] == 'null') {
			$warn[] = 'Debe seleccionar un Nivel de Acceso!';
		}
		
		if(count($warn)) {
			echo '<div class="warning">';
			echo implode('<br />', $warn);
			echo '</div>';
			//unset($warn);
		}
		
		if(!count($warn)) {
			$suc[] = 'El Volutario ha sido agregado al sistema con éxito.';
		}
		
		if(count($suc)) {
			echo '<div class="success">';
			echo implode('<br />', $suc);
			echo '</div>';
			unset($suc);
			
			//ADD THE VOLUNTEER TO THE VOLUNTEERS TABLE
			
			$sql = "INSERT INTO volunteers (volunteer_email, volunteer_password, volunteer_first_name, volunteer_last_name, "
			."volunteer_ethnicity_id, volunteer_sex_id, volunteer_acl_control_id, volunteer_date_of_birth, volunteer_force_password_change)"
			." VALUES"
			." ('$volunteer_email', '$set_password', '$volunteer_firstname', '$volunteer_lastname', "
			."'$volunteer_ethnicity', '$volunteer_sex', '$volunteer_acl_control_id', '$_POST[volunteer_date_of_birth]', '$_POST[require_reset]')";
			
			mysql_query($sql);
			echo mysql_error();
			
			$new_volunteer_id = mysql_insert_id();
			
			//POPULATE THE DETAILS TABLE

			$sql = "SELECT * FROM volunteer_detail_items ORDER BY volunteer_detail_weight";
			$result = mysql_query($sql);
			$num = mysql_num_rows($result);
			$i = 0;
			while ($i < $num) {		
			
				$volunteer_detail_item_common_name = mysql_result($result, $i, "volunteer_detail_item_common_name");
				$data_from_post = mysql_real_escape_string($_POST[$volunteer_detail_item_common_name]);
				
				$sql_a = "INSERT INTO volunteer_details (volunteer_detail_volunteer_id, volunteer_detail_item_common_name, volunteer_detail_data) "
				."VALUES ('$new_volunteer_id', '$volunteer_detail_item_common_name', '$data_from_post')";
				
				mysql_query($sql_a);
				echo mysql_error();
			
				$i++;
			}
		}
		
	}
?>
<form name="add_volunteer" method="post" action="">
	<div id="section_470_left">
		<a href="?p=manage_volunteers" class="k-button">Administrar Voluntarios</a>
		<h3>Información de Login</h3>
		<hr />
		<table>
			<tr><th align="right" width="150">Dirección de email:</th><td><input type="email" name="volunteer_email" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_email'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" data-email-msg="El formato de email no es válido"/></td></tr>
			<tr><th align="right" width="150">Contraseña:</th><td><input type="password" name="volunteer_password" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_password'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo"/></td></tr>
			<tr><th align="right" width="150">Confirmar Contraseña:</th><td><input type="password" name="volunteer_confirm_password" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_confirm_password'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></td></tr>
			<tr><th align="right">Cambiar Contraseña<em id="important_red">* en el siguiente login</em> :</th><td><input type="checkbox" value="1" name="require_reset" /></td></tr>
		</table>
		<h3>Detalles requeridos</h3>
		<hr />
		<table>
			<tr><th align="right">Nombre:</th><td colspan="3"><input type="text" name="volunteer_firstname" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_firstname'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></tr>
			
			<tr><th align="right">Apellido:</th><td colspan="3"><input type="text" name="volunteer_lastname" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_lastname'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></tr>


			<tr><th align="right">Número de Documento:</th><td><input type="text" name="volunteer_numero_documento" size="13" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_numero_documento'].'"'; } ?> id="numeroDocumento" required title="Por favor llene este campo" data-email-msg="A DOB is a must!" /></td><th>Tipo:</th>
				<td width="140">
				<select name="volunteer_tipo_documento" id="input_full">
				<option value="null">Seleccione Uno...</option>
				<?php
				$sql = "SELECT * FROM tipo_documento";
				$result = mysql_query($sql);
				$num = mysql_num_rows($result);
				$i = 0;
				while ($i < $num) {
					$tipo_documento_id = mysql_result($result, $i, "tipo_docuento_id");
					$tipo_documento = mysql_result($result, $i, "tipo_documento_nombre");
					
					echo '<option value="'.$tipo_documento_id.'">'.$tipo_documento.'</option>';
					$i++;
				}
				?>
				</select>
				</td>
			</tr>




		<tr><th align="right">Fecha de Nacimiento:</th><td><input type="date" name="volunteer_date_of_birth" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_date_of_birth'].'"'; } ?> id="dateOfBirth" required title="Por favor llene este campo" data-email-msg="A DOB is a must!" /></td>
				<th align="right">Sexo:</th>
				<td>
				<select name="volunteer_sex" id="input_full">
				<option value="null">Seleccione Uno...</option>
				<?php
				$sql = "SELECT * FROM sexies";
				$result = mysql_query($sql);
				$num = mysql_num_rows($result);
				$i = 0;
				while ($i < $num) {
					$sex_id = mysql_result($result, $i, "sex_id");
					$sex = mysql_result($result, $i, "sex_name");
					
					echo '<option value="'.$sex_id.'">'.$sex.'</option>';
					
					$i++;
				}
				?>
				</select>
				</td>
			</tr>

<tr><th align="right">Dirección:</th><td colspan="3"><input type="text" name="volunteer_address" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_address'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></tr>

<tr><th align="right">Facebook:</th><td colspan="3"><input type="text" name="volunteer_facebook" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_facebook'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></tr>

<tr><th align="right">Teléfono (en caso de emergencia):</th><td colspan="3"><input type="text" name="volunteer_phone" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_phone'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></tr>

<tr><th align="right">Teléfono/Celular:</th><td colspan="3"><input type="text" name="volunteer_cell_phone" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_cell_phone'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></tr>

<tr><th align="right">Grupo y factor de sangre:</th><td colspan="3"><input type="text" name="volunteer_blood_type" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_blood_type'].'"'; } ?>class="k-textbox" id="input_full" required title="Por favor llene este campo" /></tr>

<tr><th align="right">Fecha de Inscripción:</th><td><input type="date" name="volunteer_inscription_date" <?php if(count($warn)) { echo 'value="'.$_POST['volunteer_inscription_date'].'"'; } ?> id="inscriptionDate" required title="Por favor llene este campo" data-email-msg="A DOB is a must!" /></td>


			<tr>
				<th>Nivel de Acceso:</th>
				<td>
				<select name="volunteer_acl_control_id" id="input_full">
				<option value="null">Seleccione Uno...</option>
				<?php
				$sql = "SELECT * FROM acl_controls";
				$result = mysql_query($sql);
				$num = mysql_num_rows($result);
				$i = 0;
				while ($i < $num) {
					$acl_control_id = mysql_result($result, $i, "acl_control_id");
					$acl_control_name = mysql_result($result, $i, "acl_control_name");
					
					echo '<option value="'.$acl_control_id.'">'.$acl_control_name.'</option>';
					
					$i++;
				}
				?>
				</select>
				</td>
				
			</tr>
	
		</table>
	</div>
<!--
	<div id="section_470_right">
		<h3>Detalles Personalizados</h3>
		<hr />
		<table>
			<?php
/*
			$sql = "SELECT * FROM volunteer_detail_items ORDER BY volunteer_detail_weight";
			$result = mysql_query($sql);
			$num = mysql_num_rows($result);
			$i = 0;
			while ($i < $num) {
			
				$volunteer_detail_item_common_name = mysql_result($result, $i, "volunteer_detail_item_common_name");
				$volunteer_detail_item_display_name = mysql_result($result, $i, "volunteer_detail_item_display_name");
				
				if(count($warn)) {
				

				echo '<tr><th align="right">'.$volunteer_detail_item_display_name.':</th><td><input type="text" name="'.$volunteer_detail_item_common_name.'" value="'.$_POST[$volunteer_detail_item_common_name].'" class="k-textbox" id="input_full" /></td></tr>';
				
				} else { 
				
					echo '<tr><th align="right">'.$volunteer_detail_item_display_name.':</th><td><input type="text" name="'.$volunteer_detail_item_common_name.'" class="k-textbox" id="input_full" /></td></tr>';
				}
				$i++;
			}
*/
			?>
		</table>
	</div>
-->


	<div id="section_470_right">
		<h3>Cursos</h3>
		<hr />
		<select name="skills" id="skill_combo">
		
		
		<?php
			$sql = "SELECT * FROM volunteer_skill_items ORDER BY volunteer_skill_item_weigth";
			$result = mysql_query($sql);
			$num = mysql_num_rows($result);
			$i = 0;
			while ($i < $num) {
			
				$volunteer_skill_item_id = mysql_result($result, $i, "volunteer_skill_item_id");
				$volunteer_skill_item_display_name = mysql_result($result, $i, "volunteer_skill_item_display_name");
				
				if(count($warn)) {
					echo '<tr><th align="right">'.$volunteer_skill_item_display_name.':</th><td><input type="combobox" 				name="skill_'.$volunteer_skill_item_id.'" value="'.$_POST[$volunteer_skill_item_id].'"  id="input_full" /></td></tr>';
			
				} else { 
				
					echo '<option value="skill_id_'.$volunteer_skill_item_id.'">'.$volunteer_skill_item_display_name.'</option>';
				}
				$i++;
			}
			?>
		</select>
		<input type='button' value='+' onclick='crear_skill(this)' />
		<table id='skill_table'>
		<tr><th align='rigth' width='50%'>Título</th><th align='rigth' width='5%'>Aprobado</th><th align='rigth' width='5%'>Cursado</th><th></th></tr>
			
		</table>
		<span id='out_span'>Salida</span>
	</div>


	
<div id="clear"></div>
<br />
<br />

<hr />
<p>Por favor complete las secciones <em id="important_red">Información de Login</em> y <em id="important_red">Detalles Requeridos</em> antes de enviar este formulario.
<br />
<br />

<input type="submit" name="submit" id="submit" value="Enviar" class="k-button"/>
</form>
<script>
$(document).ready(function(){
	$("#dateOfBirth").kendoDatePicker({ format: "yyyy-MM-dd" });
	$("#inscriptionDate").kendoDatePicker({ format: "yyyy-MM-dd" });
    var validatable = $("#add_volunteer").kendoValidator().data("kendoValidator");
    	$("#submit").click(function() {
    		if (validatable.validate()) {
        		save();
         	}
	});

});
</script>
<?php 
} else { 
	echo 'No tienes acceso a este recurso.';
} 
?>


<?php
	if(count($warn)) {
		unset($warn);
	}
	if(count($suc)) {
		unset($suc);
	}
?>
